package com.c.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.FoodGroup;
import com.c.entity.SportGroup;
import com.c.mapper.FoodGroupMapper;
import com.c.mapper.SportGroupMapper;
import com.c.service.FoodGroupService;
import com.c.service.SportGroupService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-25-12:30
 */
@Service
@Transactional
public class FoodGroupServiceImpl extends ServiceImpl<FoodGroupMapper, FoodGroup> implements FoodGroupService {

    /**
     * 膳食方案分页查询(优化分页查询)
     *1.统计所有的数据（必须要返回的数据）
     * 2.比较要分页查询的数据是开始位置是否大于总数据，大于则不查，小于则继续查
     * 3.根据查询条件查询出范围值的所有di数组
     * 4.拿着对应的id数组走主键索引去查询对应的数据
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery2(QueryPageBean queryPageBean) {
        //统计所有的数据
        Long total = baseMapper.selectCount(null);
        if(total>=queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1)){
            Integer[] foodgroupids=baseMapper.selectIdsByCondtion(queryPageBean.getQueryString(),queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1),queryPageBean.getPageSize());
            if(foodgroupids==null){
                return null;
            }
            List<FoodGroup> FoodGroups=baseMapper.selectFoodGroup(foodgroupids);
            return new PageResult(total,FoodGroups);
        }
        return null;
    }

    /**
     * 新增运动方案，同时需要让运动方案组关联运动项
     *
     * @param foodGroup
     * @param fooditemIds
     */
    @Override
    public void add(FoodGroup foodGroup, Integer[] fooditemIds) {
        //新增运动组，操作t_sportGroup表
        baseMapper.insert(foodGroup);
        //设置运动组和运动项的多对多的关联关系，操作t_sportgroup_sportitem表
        Integer foodGroupId = foodGroup.getId();
        this.setFoodGroupAndFoodItem(foodGroupId, fooditemIds);
    }

    @Override
    public List<Integer> findFoodItemIdsByFoodGroupId(Integer id) {
        return baseMapper.findFoodItemIdsByFoodGroupId(id);
    }

    /**
     * 建立运动组和运动项多对多关系
     *
     * @param foodGroupId
     * @param fooditemIds
     */
    private void setFoodGroupAndFoodItem(Integer foodGroupId, Integer[] fooditemIds) {
        if (fooditemIds != null && fooditemIds.length > 0) {
            Map<String, Integer> map = null;
            for (Integer fooditemId : fooditemIds) {
                map = new HashMap<>();
                map.put("foodGroupId", foodGroupId);
                map.put("fooditemId", fooditemId);
                baseMapper.setFoodGroupAndFoodItem(map);
            }
        }
    }


    /**
     * 编辑运动组信息，同时需要关联运动项
     *
     * @param foodGroup
     * @param fooditemIds
     */
    @Override
    public void edit(FoodGroup foodGroup, Integer[] fooditemIds) {
        //修改检查组基本信息，操作检查组t_checkgroup表
        baseMapper.updateById(foodGroup);
        //清理当前检查组关联的检查项，操作中间关系表t_checkgroup_checkitem表
        baseMapper.deleteAssociation(foodGroup.getId());
        //重新建立当前检查组和检查项的关联关系
        Integer foodGroupId = foodGroup.getId();
        this.setFoodGroupAndFoodItem(foodGroupId, fooditemIds);
    }

    /**
     * 删除运动组
     *
     * @param id
     */
    @Override
    public Result deleteById(Integer id) {
            //1 删除检查组和检查项的关联表(t_checkgroup_checkitem)的数据,根据检查组id进行删除
            baseMapper.deleteAssociation(id);
            //2删除检查组
            baseMapper.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);

    }

}
